package SpringBoot.__template.web集成FreeMarker.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.util.ResourceUtils;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.titan.core.utils.io.ConsoleUtils;

/**
 *
 *
 * @author 周成功
 *
 */
@EnableWebMvc  
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
    @Override  
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        String location = this.getClass().getPackage().getName();
        if(location.lastIndexOf('.')>0) {
            location = "/" + location.substring(0, location.lastIndexOf('.'));
        } else {
            location = "";
        }
        location = location.replaceAll("\\.", "/") + "/web/";
        location = ResourceUtils.CLASSPATH_URL_PREFIX + location;
        
        String[] exts = new String[]{"html", "htm", "css", "js", "ico", "png", "jpg", "gif", "swf", "ttf", "woff", "woff2"};
        for (int i = 0; i < exts.length; i++) {
            registry.addResourceHandler("/**/*."+exts[i]+"*").addResourceLocations(location);  
        }
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX+"/META-INF/resources/webjars/");
        super.addResourceHandlers(registry);
    }
    

    
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
//        registry.addViewController("/index");
//        registry.addViewController("/user");
//        registry.addRedirectViewController("/index", "/templates/index.html");
        registry.addRedirectViewController("/", "/index.html");
//        registry.addStatusController("/403", HttpStatus.FORBIDDEN);
        super.addViewControllers(registry);
    }    
}
